﻿using CommonServiceLocator;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Jn698._45GWTool.Models.DataGrid;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using WebFirst.Entities;

namespace Jn698._45GWTool.ViewModels
{
    public class PlanContentViewModel:ViewModelBase
    {
		private SingleData plan;

		public SingleData Plan
		{
			get { return plan; }
			set { plan = value; RaisePropertyChanged(); }
		}

		private string number;

		public string Number
		{
			get { return number; }
			set { number = value; RaisePropertyChanged(); }
		}

		private string address;

		public string Address
		{
			get { return address; }
			set { address = value; RaisePropertyChanged(); }
		}

		private string startID;

		public string StartID
		{
			get { return startID; }
			set { startID = value; RaisePropertyChanged(); }
		}

		private string endID;

		public string EndID
		{
			get { return endID; }
			set { endID = value; RaisePropertyChanged(); }
		}

		private int waitFlag;

		public int WaitFlag
		{
			get { return waitFlag; }
			set { waitFlag = value; RaisePropertyChanged(); }
		}

		private ObservableCollection<string> waitList;

		public ObservableCollection<string> WaitList
		{
			get { return waitList; }
			set { waitList = value; RaisePropertyChanged(); }
		}

		private string delayTime;

		public string DelayTime
		{
			get { return delayTime; }
			set { delayTime = value; RaisePropertyChanged(); }
		}

		private int resultSelected;

		public int ResultSelected
		{
			get { return resultSelected; }
			set { resultSelected = value; RaisePropertyChanged(); }
		}

		private ObservableCollection<string> resultList;

		public ObservableCollection<string> ResultList
		{
			get { return resultList; }
			set { resultList = value; RaisePropertyChanged(); }
		}

		private string attrByte;

		public string AttrByte
		{
			get { return attrByte; }
			set { attrByte = value; RaisePropertyChanged(); }
		}

		private string subStart;

		public string SubStart
		{
			get { return subStart; }
			set { subStart = value; RaisePropertyChanged(); }
		}

		private string subLen;

		public string SubLen
		{
			get { return subLen; }
			set { subLen = value; RaisePropertyChanged(); }
		}
		private ObservableCollection<RCSDDataModel> rcsdList;

		public ObservableCollection<RCSDDataModel> RcsdList
        {
			get { return rcsdList; }
			set { rcsdList = value; }
		}

        public PlanContentViewModel()
        {
			RcsdList = new ObservableCollection<RCSDDataModel>();
			WaitList = new ObservableCollection<string>() { "false","true"};
			ResultList = new ObservableCollection<string>() { "不比对","比","比对上报"};
            Plan = ServiceLocator.Current.GetInstance<SingleData>();
			if (plan.Name==null)
			{
				return;
			}
			var datas = plan.Name.Split(',');
			if (datas.Length<10)
			{
				return;
			}
			Number = datas[0];
			Address = datas[1];
			StartID = datas[2];
			EndID = datas[3];
			WaitFlag = int.Parse(datas[4]);
			DelayTime = datas[5];
			ResultSelected = int.Parse(datas[6]);
			AttrByte = datas[7];
			SubStart = datas[8];
			SubLen = datas[9];
            int num = (datas.Length-10) / 2;

            for (int i = 0; i < num; i++)
            {
                RCSDDataModel rCSDDataModel = new RCSDDataModel();
                rCSDDataModel.CSD = datas[10+2*i];
                rCSDDataModel.Data = datas[10 + 2*i + 1];
                RcsdList.Add(rCSDDataModel);
            }
        }

		public RelayCommand<Window> OKCommand
		{
			get
			{
				var command = new RelayCommand<Window>((w) =>
				{
					StringBuilder stringBuilder = new StringBuilder();
					stringBuilder.Append(Number);
					stringBuilder.Append(",");
                    stringBuilder.Append(Address);
                    stringBuilder.Append(",");
                    stringBuilder.Append(StartID);
                    stringBuilder.Append(",");
                    stringBuilder.Append(EndID);
                    stringBuilder.Append(",");
                    stringBuilder.Append(WaitFlag.ToString());
                    stringBuilder.Append(",");
                    stringBuilder.Append(DelayTime);
                    stringBuilder.Append(",");
                    stringBuilder.Append(ResultSelected.ToString());
                    stringBuilder.Append(",");
                    stringBuilder.Append(AttrByte);
                    stringBuilder.Append(",");
                    stringBuilder.Append(SubStart);
                    stringBuilder.Append(",");
                    stringBuilder.Append(SubLen);
					int num = RcsdList.Count;
					for (int i = 0; i < num; i++)
					{
						RCSDDataModel rCSDDataModel = RcsdList[i];
                        stringBuilder.Append(",");
                        stringBuilder.Append(rCSDDataModel.CSD);
                        stringBuilder.Append(",");
                        stringBuilder.Append(rCSDDataModel.Data);
                    }
					Plan.Name = stringBuilder.ToString();
                    w.DialogResult = true;
				});
				return command;
			}
		}

        public RelayCommand<Window> CancelCommand
        {
            get
            {
                var command = new RelayCommand<Window>((w) => {
                    w.DialogResult = false;

                });
                return command;
            }
        }
    }
}
